import re
import csv


class ReadAndWrite:
    def __init__(self, file_path):
        self.__file_path = file_path

    def read_file(self) -> str:
        try:
            with open(file=self.__file_path, mode='r', encoding='utf-8') as f:
                reader = csv.reader(f)
                return ''.join(row[0] + '\n' for row in reader if row)[:-1]
        except IOError:
            print('Error: ' + '读取错误')

    def out_put_birthday(self) -> list:
        pattern = re.compile(r'\b\d{4}-\d{2}-\d{2}\b')
        list_one_iter = pattern.findall(self.read_file())
        return [[date] for date in list_one_iter]

    def write_file(self, new_file_path) -> str:
        try:
            with open(file=new_file_path, mode='w+', encoding='utf-8', newline='') as f:
                writer = csv.writer(f)
                writer.writerows(self.out_put_birthday())
                return '写入完成'
        except IOError:
            print('Error: ' + '写入错误')


if __name__ == "__main__":
    file = ReadAndWrite('./../身份证信息.csv')
    print('身份证信息：\n' + file.read_file())
    print('正则匹配：\n' + ''.join(data[0] + '\n' for data in file.out_put_birthday()))
    print('写入操作: \n' + file.write_file('./../出生日期.csv'))
